using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using battlenet.wow.rt.domain.objects;
using battlenet.wow.rt.sdk.data.dto;
using battlenet.wow.rt.sdk.utility;

namespace battlenet.wow.rt.sdk.objects.Mapper
{
    internal class WeaponItemMapper : Remapper<WeaponItem>
    {
        private readonly SlotType _slot;

        public override void RegisterMappers()
        {
            Register(typeof(WeaponItemInfo), input => Map(input as WeaponItemInfo));
            Register(typeof(WeaponInfo), input => Map(input as WeaponInfo));
        }

        public WeaponItemMapper(SlotType slot)
        {
            _slot = slot;
        }

        internal WeaponItem Map(WeaponItemInfo info)
        {
            Debug.WriteLine(string.Format("Mapping {0}...", info.Name));
            var weapon = new WeaponItem
            {
                Slot = _slot,
                Id = info.ID,
                Name = info.Name,
                Icon = info.Icon,
                QualityId = info.Quality,
                ItemLevel = info.ItemLevel,
                TooltipParams = info.TooltipParams.Remap(new TooltipParamsMapper()),
                Stats = info.Stats.Select(s => s.Remap(new StatsMapper())).ToList(),
                Armor = info.Armor,
                Context = info.Context,
                Damage = info.WeaponInfo.Damage.Remap(new WeaponDamageMapper()),
                Dps = info.WeaponInfo.Dps,
                WeaponSpeed = info.WeaponInfo.WeaponSpeed
            };
            Debug.WriteLine(string.Format("Mapping {0} completed!", info.Name));
            return weapon;
        }

        internal WeaponItem Map(WeaponInfo info)
        {
            Debug.WriteLine(string.Format("Mapping {0}...", info.Name));
            var weapon = new WeaponItem();
            weapon.Slot = _slot;
            weapon.Id = info.ID;
            weapon.Name = info.Name;
            weapon.Icon = info.Icon;
            weapon.QualityId = info.Quality;
            weapon.ItemLevel = info.ItemLevel;
            weapon.TooltipParams = info.TooltipParams.Remap(new TooltipParamsMapper());
            weapon.Stats = info.Stats == null ? new List<Statistic>() : info.Stats.Select(s => s.Remap(new StatsMapper())).ToList();
            weapon.Armor = info.Armor;
            weapon.Context = info.Context;
            weapon.Damage = info.Damage == null ? new WeaponDamage() : info.Damage.Remap(new WeaponDamageMapper());
            weapon.Dps = info.Dps;
            weapon.WeaponSpeed = info.WeaponSpeed;
            Debug.WriteLine(string.Format("Mapping {0} completed!", info.Name));
            return weapon;
        }
    }
}